package cn.stylefeng.guns.modular.business.frequency.settlementbargain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.settlementbargain.entity.SettlementBargain;
import cn.stylefeng.guns.modular.business.frequency.settlementbargain.mapper.SettlementBargainMapper;
import cn.stylefeng.guns.modular.business.frequency.settlementbargain.model.param.SettlementBargainParam;
import cn.stylefeng.guns.modular.business.frequency.settlementbargain.service.SettlementBargainService;
import cn.stylefeng.guns.modular.business.frequency.settlementbargain.enums.SettlementBargainExceptionEnum;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 结算人列表 服务实现类
 *
 * @author yujie
 * @date 2023/05/27 16:38
 */
@Service
public class SettlementBargainServiceImpl extends ServiceImpl<SettlementBargainMapper, SettlementBargain> implements SettlementBargainService {
    @Autowired
    private BusinessUtil businessUtil;

    @Override
    public PageResult<SettlementBargain> page(SettlementBargainParam settlementBargainParam) {

        // 构造条件
        LambdaQueryWrapper<SettlementBargain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isBlank(settlementBargainParam.getName()),SettlementBargain::getName,settlementBargainParam.getName());
        queryWrapper.like(!StringUtils.isBlank(settlementBargainParam.getPhone()),SettlementBargain::getPhone,settlementBargainParam.getPhone());
        queryWrapper.eq(SettlementBargain::getTenantId,businessUtil.getTenantId());
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SettlementBargain> list(SettlementBargainParam settlementBargainParam) {

        // 构造条件
        LambdaQueryWrapper<SettlementBargain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isBlank(settlementBargainParam.getName()),SettlementBargain::getName,settlementBargainParam.getName());
        queryWrapper.like(!StringUtils.isBlank(settlementBargainParam.getPhone()),SettlementBargain::getPhone,settlementBargainParam.getPhone());
       // queryWrapper.eq(!ObjectUtil.isNull(settlementBargainParam.getLinkmanId()),SettlementBargain::getLinkmanId,settlementBargainParam.getLinkmanId());
        queryWrapper.eq(SettlementBargain::getTenantId,businessUtil.getTenantId());
        queryWrapper.eq(SettlementBargain::getLinkmanId,settlementBargainParam.getLinkmanId());
        return this.list(queryWrapper);
    }

    @Override
    public void add(SettlementBargainParam settlementBargainParam) {

        // 将dto转为实体
        SettlementBargain settlementBargain = new SettlementBargain();
        BeanUtil.copyProperties(settlementBargainParam, settlementBargain);

        this.save(settlementBargain);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SettlementBargainParam settlementBargainParam) {
        this.removeById(settlementBargainParam.getId());
    }

    @Override
    public void edit(SettlementBargainParam settlementBargainParam) {

        // 根据id查询实体
        SettlementBargain settlementBargain = this.querySettlementBargain(settlementBargainParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(settlementBargainParam, settlementBargain);

        this.updateById(settlementBargain);
    }

    @Override
    public SettlementBargain detail(SettlementBargainParam settlementBargainParam) {
        return this.querySettlementBargain(settlementBargainParam);
    }



    /**
     * 获取结算人列表
     *
     * @author yujie
     * @since 2023/05/27 16:38
     */
    private SettlementBargain querySettlementBargain(SettlementBargainParam settlementBargainParam) {
        SettlementBargain settlementBargain = this.getById(settlementBargainParam.getId());
        if (ObjectUtil.isEmpty(settlementBargain)) {
            throw new ServiceException(SettlementBargainExceptionEnum.NOT_EXISTED);
        }
        return settlementBargain;
    }
}